package com.sinosoft.ptas.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.ptas.control.dto.OrganizationData;
import com.sinosoft.ptas.dao.TargetAttributeMapper;
import com.sinosoft.ptas.entity.TargetAttOrgRefEntity;
import com.sinosoft.ptas.entity.TargetAttributeEntity;
import com.sinosoft.ptas.service.TargetAttributeService;
import com.sinosoft.ptas.system.commom.CommonConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;

/**
 * 目标属性接口业务实现
 * @author lianWuJun
 * @time   2017年2月28日 上午11:14:05
 */
public class TargetAttributeServiceImpl implements TargetAttributeService{

	TargetAttributeMapper targetAttributeMapper;

	public void setTargetAttributeMapper(TargetAttributeMapper targetAttributeMapper) {
		this.targetAttributeMapper = targetAttributeMapper;
	}

	/**
	 * 创建目标属性
	 * @param targetAttribueEntity
	 * @throws BusinessException
	 */
	public String createTargetAttribute(TargetAttributeEntity targetAttributeEntity) throws BusinessException {
		if(targetAttributeEntity==null){
			throw new BusinessException(ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		String id=UUID.randomUUID().toString();
		targetAttributeEntity.setId(id);
		targetAttributeMapper.createTargetAttribute(targetAttributeEntity);
		List<String> taIds=new ArrayList<String>();
		taIds.add(id);
		TargetAttOrgRefEntity targetAttOrg=new TargetAttOrgRefEntity();
		createTargetAttributeOrganizationRef(targetAttOrg,taIds,targetAttributeEntity.getOid());
		return id;
	}

	/**
	 * 根据目标属性编号查询目标属性
	 * @param id
	 * @return 目标属性实体
	 */
	public TargetAttributeEntity retrieveTargetAttributeById(String id) {
		if(StringUtil.isBlank(id)){
			throw new  BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		
		return targetAttributeMapper.retrieveTargetAttributeById(id);
	}

	/**
	 * 根据目标属性的各个属性查询目标属性
	 * 如果传入参数为空，则查询全部信息
	 * @param targetAttribueEntity
	 * @return 目标属性实体集合
	 */
	public List<TargetAttributeEntity> retrieveTargetAttributes(TargetAttributeEntity targetAttributeEntity) {
		return targetAttributeMapper.retrieveTargetAttributes(targetAttributeEntity);
	}

	/**
	 * 根据目标属性编号更新目标属性
	 * @param targetAttribueEntity
	 * @throws BusinessException
	 */
	public void updateTargetAttribute(TargetAttributeEntity targetAttributeEntity) throws BusinessException {
		if(targetAttributeEntity==null){
			throw new BusinessException(ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		targetAttributeMapper.deleteTargetAttributeRefByTaId(targetAttributeEntity.getId());
		List<String> taIds=new ArrayList<String>();
		taIds.add(targetAttributeEntity.getId());
		TargetAttOrgRefEntity targetAttOrg=new TargetAttOrgRefEntity();
		createTargetAttributeOrganizationRef(targetAttOrg,taIds,targetAttributeEntity.getOid());
		targetAttributeMapper.updateTargetAttribute(targetAttributeEntity);
	}
	
	/**
	 * 根据目标属性编号更新目标属性(适用于添加目标种类表后的更新)
	 * @param targetAttribueEntity
	 * @throws BusinessException
	 */
	public void updateTargetAttributepc(TargetAttributeEntity targetAttributeEntity) throws BusinessException {
		if(targetAttributeEntity==null){
			throw new BusinessException(ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		TargetAttOrgRefEntity taore=new TargetAttOrgRefEntity();
		taore.setOid(targetAttributeEntity.getOid());
		taore.setTaid(targetAttributeEntity.getId());
		targetAttributeMapper.updateTargetAttributeOrganizationRef(taore);
		targetAttributeMapper.updateTargetAttribute(targetAttributeEntity);
	}

	/**
	 * 根据目标属性编号删除目标属性
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteTargetAttributeById(String id) throws BusinessException {
		if(StringUtil.isBlank(id)){
			throw new  BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		} 
		targetAttributeMapper.deleteTargetAttributeRefByTaId(id);
		targetAttributeMapper.deleteTargetAttributeById(id);
		
	}
	
	/**
	 * 根据项目类型编号删除目标属性
	 * @param pcid
	 * @throws BusinessException
	 */
	public void deleteTargetAttributeByPcid(String pcid)throws BusinessException {
		targetAttributeMapper.deleteTargetAttributeByPcid(pcid);
	}

	/**
	 * 创建目标属性其他司的对应关系
	 * 目标属性与其他司的关系是多对一
	 * @param taIds 
	 * @param oid
	 * @param TargetAttOrgRefEntity 用来传时间和创建者
	 * @throws BusinessException
	 */         
	public void createTargetAttributeOrganizationRef(TargetAttOrgRefEntity TargetAttOrgRefEntity,List<String> taIds, String oid) throws BusinessException {
		if(StringUtil.isBlank(oid)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		/*目标属性编号不为空  先删除目标属性与司局的关系 再创建目标属性与司局的关系 */
		if(taIds.size()!=0){
			targetAttributeMapper.deleteTargetAttOrgRefByOrgId(oid);//根据其他司编号删除目标属性			
			/*创建目标属性*/
			List<TargetAttOrgRefEntity> TargetAttOrgRefEntitys=new ArrayList<TargetAttOrgRefEntity>();
			for(int i=0;i<taIds.size();i++){
				TargetAttOrgRefEntity targetAttOrgRefEntity1=new  TargetAttOrgRefEntity();
				targetAttOrgRefEntity1.setId(UUID.randomUUID().toString());
				targetAttOrgRefEntity1.setTaid(taIds.get(i));
				targetAttOrgRefEntity1.setOid(oid);
				targetAttOrgRefEntity1.setCreater(TargetAttOrgRefEntity.getCreater());
				targetAttOrgRefEntity1.setCreateTime(TargetAttOrgRefEntity.getCreateTime());
				TargetAttOrgRefEntitys.add(targetAttOrgRefEntity1);
			}
			targetAttributeMapper.createTargetAttributeOrganizationRef(TargetAttOrgRefEntitys);
		}else{//目标属性编号为空  删除目标属性与司局的关系 再
			targetAttributeMapper.deleteTargetAttOrgRefByOrgId(oid);
		}
		
	}

	/**
	 * 根据实体创建关系（仅适用于更新，补全关系）
	 * @param TargetAttOrgRefEntity
	 * @throws BusinessException
	 */
	public void createTargetAttributeOrganizationRefupdate(TargetAttOrgRefEntity TargetAttOrgRefEntity) throws BusinessException {
		List<TargetAttOrgRefEntity> TargetAttOrgRefEntitys=new ArrayList<TargetAttOrgRefEntity>();
		TargetAttOrgRefEntitys.add(TargetAttOrgRefEntity);
		targetAttributeMapper.createTargetAttributeOrganizationRef(TargetAttOrgRefEntitys);
	}
	
	/**
	 * 根据目标属性编号查询目标属性和司局的对应关系
	 * @param taid
	 * @return 目标属性和司局对应关系实体
	 */
	public TargetAttOrgRefEntity retieveTargetAttributeOrganizationRef(String taid) {
		return targetAttributeMapper.retieveTargetAttributeOrganizationRef(taid);
	}
	
	
	/**
	 * 根据目标属性编号查询司局
	 * @param taId
	 * @param identify   标识  0代表省司  1代表其他司
	 * @return 其他司实体集合
	 */
	public List<OrganizationData> retrieveOrganizationByTaId(String taId,
			String identify) {
		if(StringUtil.isBlank(taId)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		identify=CommonConstant.OTHER_IDENTIFY;		
		return targetAttributeMapper.retrieveOrganizationByTaId(taId, identify);
	}

	/**
	 * 根据其他司编号查询目标属性
	 * @param orgId
	 * @return 目标属性实体集合
	 */
	public List<TargetAttributeEntity> retrieveTargetAttributeByOrgId(
			String orgId) {
		if(StringUtil.isBlank(orgId)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		return targetAttributeMapper.retrieveTargetAttributeByOrgId(orgId);
	}

	public void createTargetAttributeOrganizationRef(
			TargetAttOrgRefEntity TargetAttOrgRefEntity, String[] taIds,
			String oid) throws BusinessException {
	}

	/**
	 * 根据项目类型编号查询目标属性
	 * @param pcid
	 * @return 目标属性实体集合
	 */
	public List<TargetAttributeEntity> retrieveTargetAttributeBypcid(String pcid){
		return targetAttributeMapper.retrieveTargetAttributeBypcid(pcid);
	}
}
